Programming languages

Communication 1

No lectures on the 21st of November 2018

Communication 2

First laboratory lecture on the 22nd of November 2018

Download:

We will install properly them all together during the first laboratory session

Any question about the previous lecture?

Historic hero: Grace Hopper

She was a computer scientist

The first programmer of the Harvard Mark I - another general purpose electromechanical computer used during the WWII

Main developer of COBOL, one of the first high-level programming languages

COBOL adopted English for commands: x IS GREATER THAN y

A timeline

Python

It is an high-level programming language for general-purpose programming

It is currently one of the most used languages for programming in the Web, for Data Science and Natural Language Processing tasks

It will be introduced with more details during the following lectures and, in particular, in the laboratory sessions

Play with Python online:

Learning by example

Our first algorithm: take in input three different strings, i.e. two words and a bibliographic entry of a published paper, and return 2 if both the words are contained in the bibliographic entry, 1 if at least one of the words is contained in the bibliographic entry, and 0 otherwise

Defining algorithms: functions

def <algorithm>(<parameter_1>, <parameter_2>, ...)

<algorithm> and <parameter_i> cannot contain spaces and cannot start with a number

def contains_word(first_word, second_word, bib_entry):
    ...
    ...
    ...

All the instructions of the algorithm must be specified in the following lines, as an indented block

A variable is a symbolic name that contains some information referred to as a value (e.g. first_word, which is a particular kind of variable, called parameter)

A first step

Partial algorithm: if the first input word is contained in the bibliographic entry, then the number 1 is returned, otherwise 0 is returned

We need:

  • a mechanism to return a particular value if a specific condition is true

  • a way for checking if the input word is contained in the bibliographic reference

  • a command for returning the result

Conditional block

It allows one to execute a particular instruction if a condition is true (the if statement), while an alternative set of instructions is executed instead if the condition specified is false (the else statement, which is optional)

if <condition>:
    ...
    ...
else:
    ...
    ...

Specify the condition

The command in is used to check if a certain string is contained in another one

<string1> in <string2> would be true if the value <string1> is contained in <string2>

A string is a particular type of value that records a sequence of characters, and it is usually defined by using the quotes – e.g. "Berners-Lee"

Return statement

return <value_to_return>

The execution of a return statement finishes the whole execution of a function, and all the instructions that follow that statement are not processed anymore

E.g.: return 1

A number is defined by writing it down as it is – e.g. 42 and -42 for positive/negative integers, 1.625 and -1.625 for positive/negative decimals

"42" is different from 42

Partial algorithm

def contains_word(first_word, second_word, bib_entry):
    if first_word in bib_entry:
        return 1
    else:
        return 0

Boolean values

The description of the algorithm says to return 2 if both the input words are contained in the bibliographic entry

A boolean can be assigned to one out of two distinct and disjoint values, True and False

E.g.: first_word in bib_entry returns a boolean value

Logical operations

<operator> <B1>, where <operator> can be only not

<B1> <operator> <B2>, where <operator> can be either or or and

B1 B2 not B1 B1 and B2 B1 or B2

True

True

False

True

True

True

False

False

False

True

False

True

True

False

True

False

False

True

False

False

Comparisons

Strings: <S1> <operator> <S2>

S1 S2 S1 < S2 S1 <= S2 S1 > S2 S1 >= S2 S1 == S2 S1 != S2 S1 in S2 S1 not in S2

"Alice"

"Bob"

True

True

False

False

False

True

False

True

"Alice"

"Alice"

False

True

False

True

True

False

True

False

Numbers: <N1> <operator> <N2>

N1 N2 N1 < N2 N1 <= N2 N1 > N2 N1 >= N2 N1 == N2 N1 != N2

3

4

True

True

False

False

False

True

4

4

False

True

False

True

True

False

Complete algorithm

def contains_word(first_word, second_word, bib_entry):
    if first_word in bib_entry and second_word in bib_entry:
        return 2
    else:
        if first_word in bib_entry or second_word in bib_entry:
            return 1
        else:
            return 0

Avoiding repetitions

first_word in bib_entry and second_word in bib_entry used and evaluated twice

It can be avoided defining new variables: <variable_name> = <variable_value>

For instance:

  • contains_first_word = first_word in bib_entry

  • contains_second_word = second_word in bib_entry

Collapsing else-if blocks

It is possible to collapse occurrences of else statements when these contain an if statement as their first instruction.

In this case, the else-if pair can be safely replaced by an elif statement

if <condition1>:
    ...
else:
    if <condition2>:
        ...
    else:
        ...
if <condition1>:
    ...
elif <condition2>:
    ...
else:
    ...

Final algorithm

def contains_word(first_word, second_word, bib_entry):
    contains_first_word = first_word in bib_entry
    contains_second_word = second_word in bib_entry

    if contains_first_word and contains_second_word:
        return 2
    elif contains_first_word or contains_second_word:
        return 1
    else:
        return 0

Checking the correctness

In order to be sure that the result returned by an algorithm is correct, one has to test its execution by means of some approach, e.g. by trying different kinds of input and seeing if it is behaving as expected

One of the most used and effective methods used by programmers is called Test-Driven Development (or TDD)

When one has a computational problem to solve, the first thing to develop is a test so as to check if the software to develop returns the value as expected

Test-driven development cycle

Test + algorithm template

def test_<algorithm>(<algorithm input params>, expected):
    result = <algorithm>(<algorithm input params>)
    if result == expected:
        return True
    else:
        return False


def <algorithm>(<algorithm input params>):
    return


print(test_<algorithm>(<algorithm input params 1>, <expected_1>))
print(test_<algorithm>(<algorithm input params 2>, <expected_2>))

At the very beginning, all the instruction of the algorithm will be substituted by the instruction return, so as to allow all the new tests to fail

Example

def test_contains_word(first_word, second_word, bib_entry, expected):
    result = contains_word(first_word, second_word, bib_entry)
    if expected == result:
        return True
    else:
        return False


def contains_word(first_word, second_word, bib_entry):
    contains_first_word = first_word in bib_entry
    contains_second_word = second_word in bib_entry
    ...


print(test_contains_word("a", "b", "abcd", 2))
print(test_contains_word("a", "b", "acde", 1))
print(test_contains_word("a", "b", "cdef", 0))

END Programming languages